package cn.edu.njpi.controller;

import cn.edu.njpi.model.TCollage;
import cn.edu.njpi.model.TSchoolAdmin;
import cn.edu.njpi.model.TUser;
import cn.edu.njpi.service.TCollageService;
import cn.edu.njpi.service.TSchoolAdminService;
import cn.edu.njpi.utils.InformUtils;
import cn.edu.njpi.utils.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author 朱宗辉
 * @version V1.0
 * @Package cn.edu.njpi.controller
 * @date 2022/4/20 5:57 PM
 */
@RequestMapping(value = "/api/collage")
@RestController
public class CollageController {
    @Autowired
    private TCollageService tCollageService;
    @Autowired
    private TSchoolAdminService tSchoolAdminService;

    @RequestMapping(value = "/add")
    public T addCollage(@RequestBody TCollage tCollage, @RequestHeader("token") String token) {
        T t = new T();
        TUser tUser = (TUser) InformUtils.map.get(token);
        boolean b = false;
        if (tUser.getUserPermssionId() == 2) {
            TSchoolAdmin tSchoolAdmin = tSchoolAdminService.selectTSchoolAdminByUserId(tUser.getUserId());
            if (Objects.equals(tSchoolAdmin.getSchoolId(), tCollage.getSchoolId())) {
                b = true;
            }
        }
        if (tUser.getUserPermssionId() == 1 || b) {
            t = tCollageService.addCollage(tCollage);
        } else {
            t.add("error", "您无权添加院系!");
        }
        return t;
    }

    @RequestMapping(value = "/change")
    public T changeCollage(@RequestBody TCollage tCollage, @RequestHeader("token") String token) {
        T t = new T();
        TUser tUser = (TUser) InformUtils.map.get(token);
        boolean b = false;
        if (tUser.getUserPermssionId() == 2) {
            TSchoolAdmin tSchoolAdmin = tSchoolAdminService.selectTSchoolAdminByUserId(tUser.getUserId());
            if (Objects.equals(tSchoolAdmin.getSchoolId(), tCollage.getSchoolId())) {
                b = true;
            }
        }
        if (tUser.getUserPermssionId() == 1 || b) {
            t = tCollageService.changeCollage(tCollage);
        } else {
            t.add("error", "您无权修改院系!");
        }
        return t;
    }
}
